package com.hzdesk.media.matvector.koreabatch.client.my_paperless.adapter;

import android.animation.ObjectAnimator;
import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.hzdesk.media.matvector.koreabatch.client.my_paperless.R;
import com.hzdesk.media.matvector.koreabatch.client.my_paperless.custom_view.MyPointAndLineView;
import com.hzdesk.media.matvector.koreabatch.client.my_paperless.inter.IonClickedItemListenter;
import com.hzdesk.media.matvector.koreabatch.client.my_paperless.utils.RecyclerViewUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2018/8/9.
 * 杭州席媒科技有限公司  Developers  刘传华  10
 * 佛祖镇楼 bug辟易
 */

public class MeetingAdapter<T> extends RecyclerView.Adapter<RecyclerView.ViewHolder> {


    public IonClickedItemListenter mIonClickedItemListenter = null;
    public Context tContext;
    private List<T> mlist;

    private int NORMAL_TYPE_VIEW = 0;
    private int FOOT_TYPE_VIEW = 1;


    static final int LOADING_MORE = 1;                                                              //正在加载更多
    static final int NO_MORE = 2;                                                                   //没有更多
    int footer_state = 1;                                                                           //脚布局当前的状态,默认为没有更多
    public void setmIonClickedItemListenter(IonClickedItemListenter mIonClickedItemListenter) {
        this.mIonClickedItemListenter = mIonClickedItemListenter;
    }
    public MeetingAdapter(Context mContext) {
        tContext = mContext;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        if(viewType == NORMAL_TYPE_VIEW){
            View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.metting_item_list,parent,false);
            MeetingViewHolder mCloudListHodler = new MeetingViewHolder(view);
            return mCloudListHodler;
        }else if(viewType == FOOT_TYPE_VIEW){
            View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.cloud_list_foot_item,parent,false);
            FootViewHodler footViewHodler =    new FootViewHodler(view);
            return footViewHodler;
        }
        return null;
    }

    @Override
    public void onBindViewHolder(final RecyclerView.ViewHolder holder, int position) {
        if (holder instanceof MeetingAdapter.MeetingViewHolder) {
            final MeetingViewHolder cloudListHodler = (MeetingViewHolder) holder;
            RecyclerViewUtils mRecyclerViewUtils =   new RecyclerViewUtils();
            MeetingItemAdapter    meetingItemAdapter = new MeetingItemAdapter<String>(tContext);
            mRecyclerViewUtils.initView(((MeetingViewHolder) holder).recylcerview,0,tContext, meetingItemAdapter);
            List<String> mlist = new ArrayList<>();
            for (int i = 0; i < 3; i++) {
                mlist.add("item----"+i);
            }
            meetingItemAdapter.AddItemDate(mlist);
            showRecylcerviewAnimator(cloudListHodler.iv_pack_up);
            cloudListHodler.my_poaint_and_line.reueqstMeasure();
            cloudListHodler.iv_pack_up.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    if (cloudListHodler.recylcerview.getVisibility() == View.VISIBLE) { // 可见隐藏
                        showRecylcerviewAnimator(cloudListHodler.iv_pack_up);
                        cloudListHodler.recylcerview.setVisibility(View.GONE);
                        cloudListHodler.my_poaint_and_line.reueqstMeasure();
                    }else{      // 不可见  打开
                        showRecylcerviewAnimator(cloudListHodler.iv_pack_up);
                        cloudListHodler.recylcerview.setVisibility(View.VISIBLE);
                        cloudListHodler.my_poaint_and_line.reueqstMeasure();
                    }
                }
            });
            meetingItemAdapter.setmIonClickedItemListenter(new IonClickedItemListenter() {
                @Override
                public void onClickedItem(View view) {
                 int postion =    ((MeetingViewHolder) holder).recylcerview.getChildAdapterPosition(view);
                 if(mIonClickedItemListenter != null){
                     mIonClickedItemListenter.onClickedFist(postion);
                 }
                }

                @Override
                public void onClickedFist(int postion) {

                }

                @Override
                public void onClickedSecond(int postion) {

                }
            });

        } else  if(holder instanceof MeetingAdapter.FootViewHodler){
            FootViewHodler footViewHodler = (FootViewHodler) holder;
            footShow(position, footViewHodler);
        }
    }
    /**
     * @desc 打开
     * @param iv_pack_up
     * @param  */
    private void showRecylcerviewAnimator(ImageView iv_pack_up) {
        ObjectAnimator animator = ObjectAnimator.ofFloat(iv_pack_up, "rotation", iv_pack_up.getRotation(),iv_pack_up.getRotation()+ 180).setDuration(500);
        animator .start();
    }



    @Override
    public int getItemCount() {
        return mlist == null ? 0 : mlist.size()+1 ;
    }
    @Override
    public int getItemViewType(int position) {
        //如果position加1正好等于所有item的总和,说明是最后一个item,将它设置为脚布局
        if (position + 1 == getItemCount()) {
            return FOOT_TYPE_VIEW;
        } else {
            return NORMAL_TYPE_VIEW;
        }
    }
    /**
     * 改变脚布局的状态的方法,在activity根据请求数据的状态来改变这个状态
     *
     * @param state
     */
    public void changeState(int state) {
        this.footer_state = state;
        notifyDataSetChanged();

    }
    /**
     * @desc 添加数据的方法
     * */
    public void AddItemDate(List<T> mdate){
        mlist = mdate;
        notifyDataSetChanged();
    }
    // 展示footView 的信息
    private void footShow(int position, FootViewHodler footViewHodler) {
        if(position == 0){
            footViewHodler.item_rl.setVisibility(View.GONE);
        }
        switch (footer_state){
            case LOADING_MORE:
                footViewHodler.item_rl.setVisibility(View.VISIBLE);
                footViewHodler.item_pg.setVisibility(View.VISIBLE);
                footViewHodler.tv_loding.setVisibility(View.VISIBLE);
                footViewHodler.tv_loding.setText("加载中...");
                break;
            case NO_MORE:
                footViewHodler.item_rl.setVisibility(View.VISIBLE);
                footViewHodler.item_pg.setVisibility(View.GONE);
                footViewHodler.tv_loding.setVisibility(View.VISIBLE);
                footViewHodler.tv_loding.setText("---地主家也没余粮了--");
                break;
        }
    }
    class MeetingViewHolder extends RecyclerView.ViewHolder{

        private final RecyclerView recylcerview;
        private final ImageView iv_pack_up;
        private final TextView tv_desc;
        private final TextView tv_month;
        private final MyPointAndLineView my_poaint_and_line;

        public MeetingViewHolder(View itemView) {
            super(itemView);
            recylcerview = itemView.findViewById(R.id.metting_item_list_recylcerview);
            iv_pack_up = itemView.findViewById(R.id.metting_item_list_iv_pack_up);
            tv_desc = itemView.findViewById(R.id.metting_item_list_tv_desc);
            tv_month = itemView.findViewById(R.id.metting_item_list_tv_month);
            my_poaint_and_line = itemView.findViewById(R.id.metting_item_list_my_poaint_and_line);
        }
    }
    class FootViewHodler extends RecyclerView.ViewHolder{

        private final TextView tv_loding;
        private final ProgressBar item_pg;
        private final RelativeLayout item_rl;

        public FootViewHodler(View itemView) {
            super(itemView);
            tv_loding = itemView.findViewById(R.id.cloud_list_foot_item_tv_loding);
            item_pg = itemView.findViewById(R.id.cloud_list_foot_item_pg);
            item_rl = itemView.findViewById(R.id.cloud_list_foot_item_rl);
        }
    }
}
